package com.jiezhuo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiezhuo.entity.User;
import com.jiezhuo.service.UserService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2022-09-19
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("用户注册")
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("saveUser")
    public R saveUser(User user) {
        if (StringUtils.isEmpty(user)) {
            R.err().msg("请输入用户信息");
        }
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        boolean b = userService.save(user);
        return R.ok().msg("注册成功").code(2000);
    }

    @ApiOperation("修改密码")
//    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("updatePassword")
    public R updatePassword(String userId, String oldPassword, String newPassword) {
        User byId = userService.getById(userId);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(oldPassword, byId.getPassword());
        if (matches) {
            byId.setPassword(newPassword);
            boolean b = userService.updateById(byId);
            return R.ok().msg("修改成功").code(2000);
        }
        return R.err().msg("密码错误").code(4000);
    }

    @ApiOperation("查询用户列表")
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("queryUserList")
    public R queryUserList(String[] ids, int page, int limit) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(page) || StringUtils.isEmpty(limit)) {
            return R.err().msg("未输入输入page或limit");
        }
        if (!StringUtils.isEmpty(ids) && ids.length > 0) {
            userQueryWrapper.in("id", ids);
        }
        ;
        Page<User> userPage = userService.getBaseMapper().selectPage(new Page<>(page, limit), userQueryWrapper);
        return R.err().msg("查询成功").data("userPage", userPage.getRecords());
    }


    @ApiOperation("启用禁用用户")
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("updateStatus")
    public R updateStatus(String[] ids, Integer status) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(ids) && ids.length > 0) {
            userQueryWrapper.in("id", ids);
        } else {
            return R.err().code(4000).msg("请输入id");
        }
        List<User> users = userService.getBaseMapper().selectList(userQueryWrapper);

        for (User user : users) {
            user.setStatus(status + "");
            int i = userService.getBaseMapper().updateById(user);
        }
        return R.ok().code(2000).msg("修改成功");
    }

//    @ApiOperation("删除用户")
//    @PreAuthorize("hasAuthority('admin')")
//    @PostMapping("deleteUser/{id}")
//    public R deleteUser(@PathVariable("id") Long id) {
//        User user = new User();
//        user.setId(id);
//        user.setDelFlag(1);
//        if (userService.updateById(user)) {
//            return R.ok().code(2000).msg("删除成功").data("data", null);
//        }
//        return R.err().code(4000).msg("删除失败").data("data", null);
//    }

}

